define(function () {
    'use strict';
    function ctrl($scope, $state, $ionicLoading, LocalCacheService, CommonService, FinancingService) {
        var par = $scope.param = {};
        var user = LocalCacheService.getUser();

        function init() {
            $ionicLoading.show();
            FinancingService.getQuestions(user.client_id).then(
                function (data) {
                    $ionicLoading.hide();
                    for (var i = 0; i < data.riskQuestionList.length; i++) {
                        var question = data.riskQuestionList[i];
                        var answers = new Array();
                        $.each(question.answerContentMap, function (key, value) {
                            answers.push({"key": key, "value": value});
                            question.answerContentMap = answers;
                        });
                        question.paper_answer = '';
                        question.question_idx = i + 1;
                        par.questions.push(question);
                    }
                }
            );
        }

        $scope.$on('$ionicView.beforeEnter', function () {
            par.questions = new Array();
            init();
        });

        $scope.submitClick = function () {
            if (readyToSubmit()) {
                $ionicLoading.show();
                var paper_answers = '';
                for (var i = 0; i < par.questions.length; i++) {
                    var question = par.questions[i];
                    if(i<par.questions.length-1){
                        paper_answers += question.question_no + '&' + question.paper_answer + '|';
                    }else{
                        paper_answers += question.question_no + '&' + question.paper_answer;
                    }
                }
                FinancingService.setAnswer({
                    client_id: user.client_id,
                    answers: paper_answers
                }).then(function (result) {
                        //判断返回的分数
                        //如果大于60分着跳转到协议页面
                        if (result.paper_score>result.min_score) {
                            $ionicLoading.hide();
                            FinancingService.setCreditQuota(user.client_id);
                            $state.go('tab.account-easyFinancingAgreement',{typeid:1});
                        } else {
                            //否则弹出分数提示，说明情况
                            CommonService.showAlert({message: '您的征信得分'+result.paper_score+'分，未能达到'+result.min_score+'分，请重新测评'});
                            $ionicLoading.hide();
                        }
                    }, function (result) {
                        CommonService.showAlert({message: result.error_info});
                        $ionicLoading.hide();
                    }
                );
            }
        };
        function readyToSubmit() {
            for (var i = 0; i < par.questions.length; i++) {
                var question = par.questions[i];
                if ('0' == question.question_kind && CommonService.isStrEmpty(question.paper_answer)) {
                    CommonService.showAlert({message: '第' + question.question_idx + '题未答！'});
                    return false;
                }
                if ('1' == question.question_kind) {
                    var answer_content = question.answerContentMap;
                    var paper_answer = '';
                    for (var j = 0; j < answer_content.length; j++) {
                        if (answer_content[j].checked) {
                            paper_answer += answer_content[j].key + '&';
                        }
                    }
                    if (CommonService.isStrEmpty(paper_answer)) {
                        CommonService.showAlert({message: '第' + question.question_idx + '题未答！'});
                        return false;
                    } else {
                        question.paper_answer = paper_answer.substring(0, paper_answer.length - 1);
                    }
                }
            }
            return true;
        }
    }

    ctrl.$inject = ['$scope', '$state', '$ionicLoading', 'LocalCacheService', 'CommonService', 'FinancingService'];
    return ctrl;
});